 
 import { unioformDateTransform,randomToken } from "@/utils/index.js";

 export function createUser(db){
    // 创建user数据库表
    let objectStore;
    if(!db.objectStoreNames.contains('user')){
        // id作为主键
        // objectStore = db.createObjectStore('user',{keyPath:'id'})
        // 自动生成主键 ---新增的时候，必须要指定绑定的键
        objectStore = db.createObjectStore('user',{autoIncreament:true})

        // 创建表字段   //索引名称，索引所在属性，配置对象
        objectStore.createIndex('id','id',{unique:true})
        objectStore.createIndex('userName','userName',{unique:true})
        objectStore.createIndex('roles','roles',{unique:false})
        objectStore.createIndex('createTime','createTime',{unique:true})
        objectStore.createIndex('token','token',{unique:true})
        objectStore.createIndex('isAlive','createTime',{unique:false})
        objectStore.createIndex('loginTime','createTime',{unique:true})


        // 只在建表时插入初始数据
        objectStore.add({
            "userName":"visitor",
            "roles":["visitor"],
            "createTime":unioformDateTransform(new Date()),
            "token":randomToken(),
            "isAlive":true,
            "loginTime":unioformDateTransform(new Date()),
        },"id");
    }
}

